package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.enumeration.VIPType;
import com.qire.manhua.dal.IMessageDataDAL;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.viewModel.impl.MessageLikeListViewModel;
import com.qire.manhua.model.vo.MessageLikePageVO;
import com.qire.manhua.model.vo.MessageReadVO;

public class MessageLikeListService extends DataService {

    private final IPageDataDAL pageDataDAL = WarehouseFactory.create(this, IPageDataDAL.class);
    private final IMessageDataDAL messageDataDAL = WarehouseFactory.create(this, IMessageDataDAL.class);

    private final MessageLikeListViewModel viewModel;

    public MessageLikeListService(MessageLikeListViewModel viewModel) {
        this.viewModel = viewModel;
    }

    public void loadPageData(int pageNo) {
        pageDataDAL.fetchMessageLikePageData(pageNo);
    }
    @StrategyAnnotation(event = Url.MessageLikePageData)
    public void fetchMessageLikePageDataApiCallback(MessageLikePageVO[] dataVO, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        if(ArrayUtil.isEmpty(dataVO)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        for(MessageLikePageVO vo : dataVO) {

            int id  = vo.post.post_id;
            int comicId = vo.book.book_id;
            String userName = vo.like.user_avatar.user_name;
            String userPortrait = vo.like.user_avatar.user_img;
            VIPType vipType = VIPType.build(vo.like.user_avatar.vip);
            CharSequence quotedContent = vo.post.post_content;
            boolean isQuotedDeleted = vo.post.post_del == 1;
            String comicName = vo.book.book_name;
            String giveLikeTime = vo.create_time;

            viewModel.addMessageLikeItem(id, comicId, userName, userPortrait, vipType, quotedContent, isQuotedDeleted, comicName, giveLikeTime);
        }

        viewModel.refreshUI();
        viewModel.refreshMessageLikeAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }

    /**
     * 标记列表全部已读
     */
    public void markAllRead() {
        messageDataDAL.readMessageOnType(5);
    }
    @StrategyAnnotation(event = Url.MessageReadOnType)
    public void readMessageApiCallBack(MessageReadVO dataVO, boolean isCache) {
        // 在提前做了，没什么可做的。
        if("全部已读成功".equals(dataVO.ok)) {

        }
    }

}
